package com.gitee.zzh.common;

import com.google.zxing.common.BitMatrix;

import java.awt.*;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class ImageUtils {

    public static Color getDarkColor(){
        int n = new Random().nextInt(10);
        return switch (n) {
            case 0 -> new Color(110, 52, 2);
            case 1 -> new Color(41, 32, 1);
            case 2 -> new Color(49, 104, 3);
            case 3 -> new Color(55, 69, 38);
            case 4 -> new Color(85, 92, 91);
            case 5 -> new Color(5, 74, 50);
            case 6 -> new Color(11, 3, 85);
            case 7 -> new Color(78, 3, 62);
            case 8 -> new Color(82, 4, 108);
            default -> new Color(80, 2, 26);
        };
    }

    /**
     * 拼接矩阵字符串
     */
    public static String getMatrixString(BitMatrix bitMatrix) {
        StringBuilder matrixString = new StringBuilder();
        for (int x = 0; x < bitMatrix.getWidth(); x++) {
            for (int y = 0; y < bitMatrix.getHeight(); y++) {
                matrixString.append(bitMatrix.get(x, y)? 1:0);
            }
            matrixString.deleteCharAt(matrixString.length()-1).append("\n");
        }
        return matrixString.toString();
    }

    public static String getMultiMatrixString(String matrixString, int multi) {
        String zeroRepeat = "0".repeat(multi);
        String oneRepeat = "1".repeat(multi);
        String rowMatrixString = matrixString
                .replace("0", zeroRepeat)
                .replace("1", oneRepeat);
        StringBuilder stringBuilder = new StringBuilder();
        for(String row : rowMatrixString.split("\n")) {
            String rowRepeat = (row + "\n").repeat(multi);
            stringBuilder.append(rowRepeat);
        }
        return stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
    }

    /**
     * 获取最小方块的宽度
     */
    public static int getMinWidth(BitMatrix bitMatrix) {
        int width = Integer.MAX_VALUE;
        String matrixString = getMatrixString(bitMatrix).replace("\n", "");
        String[] strings = matrixString.split("0");
        for (String string : strings) {
            int length = string.length();
            if (length > 0 && length < width) {
                width = length;
            }
        }
        return width;
    }

    /**
     * 获取小方块开始的坐标
     */
    public static Set<String> getStartIndexSet(BitMatrix bitMatrix, int minWidth) {
        Set<String> startIndexSet = new HashSet<>();
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        int y0 = 0;
        ty: for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (bitMatrix.get(x, y)) {
                    y0 = y % minWidth;
                    break ty;
                }
            }
        }

        int x0 = 0;
        tx: for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (bitMatrix.get(x, y)) {
                    x0 = x % minWidth;
                    break tx;
                }
            }
        }
        int countY = (height - y0)/minWidth;
        int countX = (width - x0)/minWidth;
        for (int y = 0; y < countY; y++) {
            for (int x = 0; x < countX; x++) {
                int x1 = x0 + x * minWidth;
                int y1 = y0 + y * minWidth;
                if (bitMatrix.get(x1, y1)) {
                    startIndexSet.add(x1 + "," + y1);
                }
            }
        }
        return startIndexSet;
    }
}
